package com.ys.erp.entity.data.bean;

import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwj
 * @date 2025/6/20
 */
    
@Data
@TableName(value = "t_wfb_order")
public class WfbOrder {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 店铺编码
     */
    @TableField(value = "shop_code")
    private String shopCode;

    /**
     * 用户id
     */
    @TableField(value = "user_id")
    private Long userId;

    /**
     * 猫咪编号
     */
    @TableField(value = "cat_code")
    private String catCode;

    /**
     * 签约门店
     */
    @TableField(value = "sign_shop_code")
    private String signShopCode;

    /**
     * 签约门店名称
     */
    @TableField(value = "sign_shop_name")
    private String signShopName;

    /**
     * 微分呗商品名称
     */
    @TableField(value = "wfb_product_name")
    private String wfbProductName;

    /**
     * 微分呗签约人手机号
     */
    @TableField(value = "wfb_phone")
    private String wfbPhone;

    /**
     * 微分呗签约人昵称
     */
    @TableField(value = "wfb_nick_name")
    private String wfbNickName;

    /**
     * 微分呗签约时间戳
     */
    @TableField(value = "wfb_sign_time")
    private Integer wfbSignTime;

    /**
     * 微分呗订购编号
     */
    @TableField(value = "wfb_subscription_no")
    private String wfbSubscriptionNo;

    /**
     * 微分呗商户 外部订单号 全平台端唯一
     */
    @TableField(value = "wfb_out_subscription_no")
    private String wfbOutSubscriptionNo;

    /**
     * 微分呗商户商品订单 支付宝后面服务提报需要
     */
    @TableField(value = "wfb_product_no")
    private String wfbProductNo;

    /**
     * 微分呗芝麻后付开通协议号
     */
    @TableField(value = "wfb_credit_agreement_id")
    private String wfbCreditAgreementId;

    /**
     * 微分呗用户uid
     */
    @TableField(value = "wfb_alipay_user_id")
    private String wfbAlipayUserId;

    /**
     * 微分呗订单状态（订单状态）
ORDERING：下单中,
ORDERED：已下单,
NORMAL：正常,
SURRENDER:已解约,
END:已完结,
CANCEL:超时取消
     */
    @TableField(value = "wfb_status")
    private String wfbStatus;

    /**
     * 微分呗已完结时间 10位 时间戳
     */
    @TableField(value = "wfb_complete_time")
    private Integer wfbCompleteTime;

    /**
     * 微分呗解约渠道 APP 用户主动解约 API 商户解约 OVERDUE 超时解约 COMPLAIN 客诉解约
     */
    @TableField(value = "wfb_surrender_channel")
    private String wfbSurrenderChannel;

    /**
     * 是否取消发送失败短息，0否，1是
     */
    @TableField(value = "is_cancel_fail_sms")
    private Integer isCancelFailSms;

    /**
     * WFB_AXF(安心付)，WFB_QKL(区块链)
     */
    @TableField(value = "wfb_type")
    private String wfbType;

    /**
     * 租户id
     */
    @TableField(value = "tenant_id")
    private Integer tenantId;

    /**
     * 二级租户id
     */
    @TableField(value = "tenant_two_id")
    private Integer tenantTwoId;

    /**
     * 解约时间
     */
    @TableField(value = "surrender_time")
    private LocalDateTime surrenderTime;

    @TableField(value = "version")
    @Version
    private Integer version;

    @TableField(value = "create_time")
    private LocalDateTime createTime;

    @TableField(value = "update_time", update = "now()")
    private LocalDateTime updateTime;


    public String getOnlyUserNo() {
        if (this.userId == null) {
            return null;
        }
        return this.userId + "-" + this.getCatCode();
    }

    @AllArgsConstructor
    @Getter
    public enum Enums {

        WFB_AXF,
        WFB_QKL

    }

    public static final Map<String, String> ENUM_MAP = new HashMap<>();

    static {
        ENUM_MAP.put(Enums.WFB_AXF.name(), "微分呗安心付");
        ENUM_MAP.put(Enums.WFB_QKL.name(), "微分呗蚂蚁链");
    }

    public Integer getProgress(List<WfbOrderWater> waterList) {
        WfbOrderWater water = waterList.stream()
                .sorted(Comparator.comparing(WfbOrderWater::getWfbPeriod))
                .filter(it -> WfbOrderWater.paidFlagList.contains(it.getWfbStatus()))
                .findFirst()
                .orElseGet(WfbOrderWater::new);
        return water.getWfbPeriod();
    }

    public Long getTotalMoney(List<WfbOrderWater> waterList) {
        return waterList.getFirst().getWfbDeductionAmount() * waterList.size();
    }

}